<script setup lang="ts">
import { useUserStore } from '@/store'
import { SearchTypeEnum } from '@/enums/SearchTypeEnum'
import { dishInfoByNameApi } from '@/service/dish'
import { Dish } from '@/types/dish'

// 获取屏幕安全区域
const { safeAreaInsets } = uni.getSystemInfoSync()
console.log(safeAreaInsets)
// #ifdef MP-ALIPAY
safeAreaInsets.top = Number(safeAreaInsets.top) + 20
// #endif

const userRef = useUserStore()
const searchValue = ref<string>('')
const back = () => {
  uni.navigateBack()
}
const props = defineProps({
  // 回调函数：参数是搜索到的菜品列表，无返回值
  onSearchSuccess: {
    type: Function as PropType<(dishList: Dish[]) => void>,
    required: true, // 父组件必须传递该回调
  },
})

const toSearch = async () => {
  console.log('searchValue', searchValue)
  const response = await dishInfoByNameApi({ page: 1, size: 10, name: searchValue.value })
  console.log(response)
  props.onSearchSuccess(response.data.records)
}
const inputSearch = (value) => {
  console.log(value.detail.value)
  searchValue.value = value.detail.value
}
</script>

<template>
  <view class="navbar" :style="{ paddingTop: Number(safeAreaInsets?.top) + 'px' }">
    <!--  #if def  MP-ALIPAY -->
    <view class="back" @tap="back"><uni-icons type="left" size="20" /></view>
    <!--  #endif -->
    <view class="search">
      <uni-icons class="icon" type="search" size="20" @tap="toSearch" />
      <input type="text" placeholder="搜索" placeholder-class="placeholder" @input="inputSearch" />
    </view>
    <navigator url="/pages/my/dish/info" open-type="navigate" hover-class="navigator-hover">
      <view class="add"><text>+</text></view>
    </navigator>
  </view>
</template>

<style lang="scss">
.navbar {
  position: relative;
  display: flex;
  flex-direction: row;

  // #ifdef MP-ALIPAY
  justify-content: start;
  height: 100rpx;
  padding: 30px 0 0 70rpx;
  padding-top: 20px;
  background-image: url(@/static/home/navigator_bg.png);
  // background-color: #90D7EC;
  background-size: cover;
  // #endif

  .logo {
    display: flex;
    align-items: center;
    height: 64rpx;
    padding-left: 30rpx;

    .logo-image {
      width: 166rpx;
      height: 39rpx;
    }

    .logo-text {
      flex: 1;
      padding-left: 20rpx;
      margin: 2rpx 0 0 20rpx;
      font-size: 26rpx;
      line-height: 28rpx;
      color: #fff;
      border-left: 1rpx solid #fff;
    }
  }
  .back {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 50rpx;
    height: 50rpx;
    padding: 0 10rpx 0 10rpx;
    margin: 16rpx 5rpx;
    font-size: 28rpx;
    color: #fff;
  }
  .search {
    display: flex;
    align-items: center;
    width: 45%;
    // justify-content: space-between;
    height: 64rpx;
    padding: 0 10rpx 0 26rpx;
    margin: 16rpx 20rpx;
    font-size: 28rpx;
    background-color: rgba(255, 255, 255, 0.5);
    border-radius: 32rpx;

    .icon {
      opacity: 0.5;
    }

    .icon-search {
      color: rgb(130, 130, 130);
    }

    .icon-scan {
      padding: 15rpx;
      font-size: 30rpx;
    }
  }
  .add {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 64rpx;
    height: 64rpx;
    margin: 16rpx 0;
    font-size: 28rpx;
    color: #fff;
    background-color: rgba(255, 255, 255, 0.5);
    border-radius: 50%;

    > text {
      color: #000;
    }
  }
}
</style>
